home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / fixes / fix_raise.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  2.4 KB  |  62 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Fixer for \'raise E, V, T\'
  5.  
  6. raise         -> raise
  7. raise E       -> raise E
  8. raise E, V    -> raise E(V)
  9. raise E, V, T -> raise E(V).with_traceback(T)
  10.  
  11. raise (((E, E\'), E\'\'), E\'\'\'), V -> raise E(V)
  12. raise "foo", V, T               -> warns about string exceptions
  13.  
  14.  
  15. CAVEATS:
  16. 1) "raise E, V" will be incorrectly translated if V is an exception
  17.    instance. The correct Python 3 idiom is
  18.  
  19.         raise E from V
  20.  
  21.    but since we can\'t detect instance-hood by syntax alone and since
  22.    any client code would have to be changed as well, we don\'t automate
  23.    this.
  24. '''
  25. from  import pytree
  26. from pgen2 import token
  27. from  import fixer_base
  28. from fixer_util import Name, Call, Attr, ArgList, is_tuple
  29.  
  30. class FixRaise(fixer_base.BaseFix):
  31.     PATTERN = "\n    raise_stmt< 'raise' exc=any [',' val=any [',' tb=any]] >\n    "
  32.     
  33.     def transform(self, node, results):
  34.         syms = self.syms
  35.         exc = results['exc'].clone()
  36.         if exc.type is token.STRING:
  37.             self.cannot_convert(node, 'Python 3 does not support string exceptions')
  38.             return None
  39.         if 'val' not in results:
  40.             new = pytree.Node(syms.raise_stmt, [
  41.                 Name('raise'),
  42.                 exc])
  43.             new.set_prefix(node.get_prefix())
  44.             return new
  45.         val = results['val'].clone()
  46.         if 'tb' in results:
  47.             tb = results['tb'].clone()
  48.             tb.set_prefix('')
  49.             e = Call(exc, args)
  50.             with_tb = Attr(e, Name('with_traceback')) + [
  51.                 ArgList([
  52.                     tb])]
  53.             new = pytree.Node(syms.simple_stmt, [
  54.                 Name('raise')] + with_tb)
  55.             new.set_prefix(node.get_prefix())
  56.             return new
  57.         return pytree.Node(syms.raise_stmt, [
  58.             Name('raise'),
  59.             Call(exc, args)], prefix = node.get_prefix())
  60.  
  61.  
  62.